package com.cro.prison.controller;/**
 * @author augus
 * @create 2022-11-07-16:04
 */

import com.cro.prison.entity.pojo.Depart;
import com.cro.prison.entity.vo.Category2Vo;
import com.cro.prison.entity.vo.DepartTreeListVo;
import com.cro.prison.service.DepartService;
import com.cro.prison.utils.Code;
import com.cro.prison.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @ClassName:DepartController
 * @Description:TODO
 * @author Augus
 * @date 2022/11/7 16:04
 * @Version 1.0
 **/

@Controller
@RequestMapping("/depart")
public class DepartController {

   @Autowired
   private DepartService departService;

   @ResponseBody
   @GetMapping("/category.json")
   public Result getCategoryJson() throws ExecutionException, InterruptedException {
      Map<Long, List<Category2Vo>> categoryJson = departService.getCatalogJson();
      return new Result(Code.GET_OK, categoryJson);
   }

   @ResponseBody
   @GetMapping("/getDepartTreeList")
   public Result getDeptTreeList() {
      List<Depart> departList = departService.list();
      List<Long> legalPlace = Arrays.asList(2L,3L,4L,5L);
      List<DepartTreeListVo> treeListVos = getVoListByPid(0L, departList).stream().filter(item -> {
         return legalPlace.contains(item.getId());
      }).collect(Collectors.toList());
      return new Result(Code.GET_OK, treeListVos);
   }

   private List<DepartTreeListVo> getVoListByPid(Long pid, List<Depart> departList) {
      List<DepartTreeListVo> voList = departList.stream().filter(item -> item.getPid() == pid).map(item -> {
         DepartTreeListVo treeListVo = new DepartTreeListVo();
         treeListVo.setId(item.getId());
         treeListVo.setName(item.getName());
         treeListVo.setChildren(getVoListByPid(item.getId(), departList));
         return treeListVo;
      }).collect(Collectors.toList());
      return voList;
   }

}
